package com.lifang.framework.context.support;

import cn.hutool.core.lang.Assert;
import com.lifang.framework.annotation.beans.Component;
import com.lifang.framework.context.annotation.ClassPathBeanDefinitionScanner;
import com.lifang.framework.beans.factory.support.DefaultListableBeanFactory;
import com.lifang.framework.context.annotation.CommonAnnotationParser;

public class AnnotationConfigApplicationContext extends AbstractRefreshableApplicationContext {

    private final Class<?>[] componentClasses;

    public AnnotationConfigApplicationContext(Class<?>... componentClasses) {
        Assert.notNull(componentClasses != null && componentClasses.length >= 1, "componentClasses not be null");
        this.componentClasses = componentClasses;
        refresh();
    }

    @Override
    protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) {
        ClassPathBeanDefinitionScanner definitionScanner = new ClassPathBeanDefinitionScanner(beanFactory);
        definitionScanner.addIncludeFilter(Component.class, new CommonAnnotationParser());
        String[] basePackage = new String[componentClasses.length];
        for (int i = 0; i < componentClasses.length; i++) {
            basePackage[i] = componentClasses[i].getPackage().getName();
        }
        definitionScanner.doScan(basePackage);
    }

}
